package com.CuandoLlegaAndroid;

import com.CuandoLlegaAndroid.Custom.CustomProgress;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
 
public class GeneralPreferences extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.general_preferences);
                // Get the custom preference
                Preference chachePreference = (Preference) findPreference("chachePreference");
                
                Preference sendFeedBack = (Preference) findPreference("sendFeedBack");
                
                Preference about = (Preference) findPreference("about");
                
                chachePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
 
                    public boolean onPreferenceClick(Preference preference) {
                    		ClearCache();
                            return true;
                    }
 
               });
                
               sendFeedBack.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                	 
                    public boolean onPreferenceClick(Preference preference) {
                    		SendMail();
                            return true;
                    }
 
               });
                
               about.setOnPreferenceClickListener(new OnPreferenceClickListener() {
               	 
                    public boolean onPreferenceClick(Preference preference) {
                    	
                    	Intent intent = new Intent().setClass(GeneralPreferences.this, About.class);
                    	startActivity(intent);
                    	return true;
                            
                    }
 
               });
        }
        
    	/**
    	 * Cleanup the cache
    	 **/
    	public void ClearCache()
    	{
    		final CustomProgress progress = new CustomProgress(this);
    		progress.ShowProgress(this.getString(R.string.limpiandoCache));

    		final Handler handler = new Handler() {
    			public void handleMessage(Message msg) 
    			{
    				progress.DismissProgress();
    			}
    		};

    		Thread checkUpdate = new Thread() {  
    			public void run() {

    				DataHelper dh = new DataHelper(GeneralPreferences.this);
    				dh.deleteAllCache();

    				handler.sendEmptyMessage(0);
    			}
    		};
    		checkUpdate.start();

    	}
    	
    	/**
    	 * Method to send feedback mail
    	 **/
    	public void SendMail()
    	{
    		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    		String aEmailList[] = { "cuandollegaandroid@gmail.com"};
    		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
    		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "[Feedback] - Cuando Llega Android");
    		emailIntent.setType("plain/text");

    		startActivity(emailIntent);

    	}
}